import random
import time

from selenium import webdriver
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.admin_pu_organization_locator import AdminPUUserOrgLocator
from Case.python_ui.Common.commontool import CommonTool
from Case.python_ui.Common.business_common_tool import BusinessCommonTools
from Case.python_ui.PageLocate.admin_pu_usergroup_locator import AdminPUUserGroupLocator
from Case.python_ui.Test_data.login_data import Logindata
from Case.python_ui.PageLocate.login_locator import Loginlocator
from Case.python_ui.Test_data.organization_data import Organization
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys


class AdminPUOrg(Basepage):

    def enter_the_org_page(self):
        doc = 'admin端进入到组织页面'
        time.sleep(5)
        for i in range(3):
            try:
                self.wait_elevisble(AdminPUUserOrgLocator.paltform_user_tab, doc)
                self.click_element(AdminPUUserOrgLocator.paltform_user_tab, doc)
                self.wait_elevisble(AdminPUUserOrgLocator.org_tab, doc)
                self.click_element(AdminPUUserOrgLocator.org_tab, doc)
                break
            except:
                self.refresh_page()
                time.sleep(5)
                continue
        time.sleep(2)
        self.wait_elevisble(AdminPUUserOrgLocator.page_total, doc)

    def wait_add_sub_deparment(self):
        doc = 'admin端等待创建组织按钮的出现'
        time.sleep(5)
        for i in range(3):
            try:
                self.wait_elevisble(AdminPUUserOrgLocator.add_sub_deparment, doc)
                self.click_element(AdminPUUserOrgLocator.add_sub_deparment, doc)
                break
            except:
                self.driver.refresh()
                self.enter_the_org_page()
                continue

    def add_sub_department(self):
        doc = 'admin端创建组织'
        time.sleep(5)
        self.refresh_page()
        self.enter_the_org_page()
        time.sleep(2)
        self.wait_add_sub_deparment()
        org_name = CommonTool.generate_string()
        self.send_keys(AdminPUUserOrgLocator.sub_deparment_name, org_name, doc)
        time.sleep(5)
        self.click_element(AdminPUUserOrgLocator.confirm_add_sub_department, doc)
        time.sleep(2)
        self.wait_elevisble(AdminPUUserOrgLocator.add_success_prompt, doc)
        self.refresh_page()
        time.sleep(5)
        self.wait_eleinvisble(AdminPUUserOrgLocator.loading_wait, doc)
        self.send_keys(AdminPUUserOrgLocator.organization_query_input, org_name, doc)
        self.click_element(AdminPUUserOrgLocator.organization_query_button, doc)
        time.sleep(5)
        self.wait_elevisble(AdminPUUserOrgLocator.sub_department_name, doc)
        self.click_element(AdminPUUserOrgLocator.sub_department_name, doc)
        time.sleep(5)
        res = self.get_element(AdminPUUserOrgLocator.non_existent_organization_name, doc).text
        print(res)
        return org_name

    def query_organization(self, org_name):
        doc = 'admin端查询组织'
        self.wait_elevisble(AdminPUUserOrgLocator.organization_query_input, doc)
        self.keyboard_event(AdminPUUserOrgLocator.organization_query_input, doc)
        time.sleep(2)
        self.send_keys(AdminPUUserOrgLocator.organization_query_input, org_name, doc)
        time.sleep(5)
        self.click_element(AdminPUUserOrgLocator.organization_query_button, doc)

    def delete_sub_departmen(self, org_name):
        doc = 'admin端删除组织'
        time.sleep(5)
        self.refresh_page()
        self.enter_the_org_page()
        self.query_organization(org_name)
        # 删除创建的部门数据
        while True:
            time.sleep(3)
            eles = self.get_elements(AdminPUUserOrgLocator.action_list, doc)
            if len(eles) != 0:
                break
            else:
                self.refresh_page()
                # self.wait_elevisble(AdminPUUserOrgLocator.loading_wait,doc)
                self.wait_eleinvisble(AdminPUUserOrgLocator.loading_wait, doc)
                time.sleep(2)
                self.send_keys(AdminPUUserOrgLocator.organization_query_input, org_name, doc)
                self.click_element(AdminPUUserOrgLocator.organization_query_button, doc)
                self.wait_elevisble(AdminPUUserOrgLocator.sub_department_name, doc)
                self.click_element(AdminPUUserOrgLocator.org_right_name, doc)
        time.sleep(5)
        self.move_mouse_to_click_element(AdminPUUserOrgLocator.action_list, doc)
        time.sleep(4)
        self.click_element(AdminPUUserOrgLocator.action_delete, doc)
        time.sleep(2)
        self.wait_elevisble(AdminPUUserOrgLocator.action_delete_confirm, doc)
        time.sleep(2)
        self.click_element(AdminPUUserOrgLocator.action_delete_confirm, doc)
        self.wait_elevisble(AdminPUUserOrgLocator.delete_success_prompt, doc)

    def query_immediate_member(self, account_email):
        doc = 'admin端查询组织下面的成员'
        time.sleep(5)
        self.refresh_page()
        self.enter_the_org_page()
        self.wait_elevisble(AdminPUUserOrgLocator.immediate_member_search_input, doc)
        self.send_keys(AdminPUUserOrgLocator.immediate_member_search_input, account_email, doc)
        time.sleep(5)
        self.click_element(AdminPUUserOrgLocator.immediate_member_query_button, doc)
        # self.wait_elevisble(OrganizationLocator.loading_wait,doc)
        self.wait_eleinvisble(AdminPUUserOrgLocator.loading_wait, doc)
        time.sleep(2)

    def member_filtering(self, org_name):
        doc = 'admin端组织下的成员筛选功能'
        time.sleep(5)
        self.refresh_page()
        self.enter_the_org_page()
        self.query_organization(org_name)
        time.sleep(5)
        self.move_mouse_to_click_element(AdminPUUserOrgLocator.filter, doc)
        time.sleep(2)
        self.click_element(AdminPUUserOrgLocator.filter_normal, doc)
        time.sleep(2)
        self.click_element(AdminPUUserOrgLocator.filter_ok, doc)
        try:
            res = self.get_element(AdminPUUserOrgLocator.filter_status, doc).text
        except StaleElementReferenceException:
            res = self.get_element(AdminPUUserOrgLocator.filter_status, doc).text
        return res

    def delete_org_member(self, org_name):
        doc = 'admin端组织下删除成员'
        time.sleep(5)
        self.refresh_page()
        self.enter_the_org_page()
        self.query_organization(org_name)
        self.wait_elevisble(AdminPUUserOrgLocator.org_member_actions_tab, doc)
        self.move_mouse_to_click_element(AdminPUUserOrgLocator.org_member_actions_tab, doc)
        eles = self.get_elements(AdminPUUserOrgLocator.actions, doc)
        for ele in eles:
            if ele.text == 'Delete':
                ele.click()
        time.sleep(5)
        self.wait_elevisble(AdminPUUserOrgLocator.delete_org_ack, doc)
        self.click_element(AdminPUUserOrgLocator.delete_org_ack, doc)
        time.sleep(2)
        self.click_element(AdminPUUserOrgLocator.member_delete_confirm, doc)
        self.wait_elevisble(AdminPUUserOrgLocator.delete_success_prompt, doc)

    def active_org_user(self, org_name):
        doc = 'admin端组织下激活成员'
        time.sleep(5)
        self.refresh_page()
        self.enter_the_org_page()
        self.query_organization(org_name)
        self.wait_elevisble(AdminPUUserOrgLocator.org_member_actions_tab, doc)
        self.move_mouse_to_click_element(AdminPUUserOrgLocator.org_member_actions_tab, doc)
        eles = self.get_elements(AdminPUUserOrgLocator.actions, doc)
        for ele in eles:
            if ele.text == 'Resend':
                ele.click()
        self.wait_elevisble(AdminPUUserOrgLocator.delete_success_prompt, doc)

    def reset_org_user_pwd(self, org_name, email, cmdopt, get_database_source):
        doc = 'admin端重置组织下的user的密码'
        time.sleep(5)
        self.refresh_page()
        self.enter_the_org_page()
        self.query_organization(org_name)
        self.wait_elevisble(AdminPUUserOrgLocator.reset_pwd, doc)
        self.move_mouse_to_click_element(AdminPUUserOrgLocator.reset_pwd, doc)
        time.sleep(2)
        self.wait_elevisble(AdminPUUserOrgLocator.change_pwd_send, doc)
        self.click_element(AdminPUUserOrgLocator.change_pwd_send, doc)
        time.sleep(2)
        change_pwd_verification_code = \
            BusinessCommonTools().get_verification_code(cmdopt, email, get_database_source)
        time.sleep(2)
        self.send_keys(AdminPUUserOrgLocator.change_password_verificationcode_input, change_pwd_verification_code, doc)
        time.sleep(2)
        self.send_keys(AdminPUUserOrgLocator.new_pwd, Logindata.reset_pwd, doc)
        time.sleep(2)
        self.send_keys(AdminPUUserOrgLocator.confirm_new_pwd, Logindata.reset_confirm_pwd, doc)
        time.sleep(2)
        self.click_element(AdminPUUserOrgLocator.reset_pwd_ok, doc)
        time.sleep(2)
        self.wait_elevisble(Loginlocator.login_username_ele, doc)

    def admin_reset_org_user_pwd(self, org_name):
        doc = '主账号重置组织下面成员的密码'
        time.sleep(5)
        self.refresh_page()
        self.enter_the_org_page()
        self.query_organization(org_name)
        time.sleep(5)
        self.wait_elevisble(AdminPUUserOrgLocator.reset_pwd, doc)
        self.move_mouse_to_click_element(AdminPUUserOrgLocator.reset_pwd, doc)
        self.wait_elevisble(AdminPUUserOrgLocator.delete_org_ack, doc)
        time.sleep(2)
        self.click_element(AdminPUUserOrgLocator.delete_org_ack, doc)
        time.sleep(2)
        self.click_element(AdminPUUserOrgLocator.edit_org_confirm, doc)
        self.wait_elevisble(AdminPUUserOrgLocator.add_success_prompt, doc)

    def edit_org_user_org_info(self, org_name, cmdopt):
        doc = '编辑组织下面成员的组织'
        time.sleep(5)
        self.refresh_page()
        self.enter_the_org_page()
        self.query_organization(org_name)
        time.sleep(5)
        self.wait_elevisble(AdminPUUserOrgLocator.operation_button, doc)
        self.move_mouse_to_click_element(AdminPUUserOrgLocator.operation_button, doc)
        time.sleep(2)
        self.get_operation_name_click('Edit Organization')
        time.sleep(5)
        self.wait_elevisble(AdminPUUserOrgLocator.edit_org_select, doc)
        self.click_element(AdminPUUserOrgLocator.edit_org_select, doc)
        time.sleep(2)
        self.send_keys(AdminPUUserOrgLocator.edit_org_input, Organization.default_organization[cmdopt]['admin'], doc)
        eles = self.get_elements(AdminPUUserOrgLocator.edit_org_to_default, doc)
        eles[0].click()
        time.sleep(2)
        self.click_element(AdminPUUserOrgLocator.edit_org_confirm, doc)
        self.wait_elevisble(AdminPUUserOrgLocator.add_success_prompt, doc)

    def get_operation_name_click(self, operation):
        doc = '通过操作的名称去点击'
        eles = self.get_elements(AdminPUUserOrgLocator.operation_type, doc)
        for ele in eles:
            if ele.text == operation:
                time.sleep(2)
                ele.click()

    def edit_org_user_user_group(self, org_name):
        doc = '编辑组织成员下的用户组'
        time.sleep(5)
        self.refresh_page()
        self.enter_the_org_page()
        self.query_organization(org_name)
        self.wait_elevisble(AdminPUUserOrgLocator.operation_button, doc)
        self.move_mouse_to_click_element(AdminPUUserOrgLocator.operation_button, doc)
        time.sleep(2)
        self.get_operation_name_click('Edit User Group')
        time.sleep(5)
        self.click_element(AdminPUUserOrgLocator.edit_org_select, doc)
        time.sleep(2)
        # self.send_keys(OrganizationLocator.edit_org_user_group_input,usergroup)
        eles = self.get_elements(AdminPUUserOrgLocator.edit_user_group, doc)
        eles[0].click()
        time.sleep(2)
        self.click_element(AdminPUUserOrgLocator.edit_org_select, doc)
        self.click_element(AdminPUUserOrgLocator.edit_org_confirm, doc)
        self.wait_elevisble(AdminPUUserOrgLocator.add_success_prompt, doc)

    def edit_org_user_user_role(self, org_name, role_name):
        doc = '编辑组织下面的成员的角色'
        time.sleep(5)
        self.refresh_page()
        self.enter_the_org_page()
        self.query_organization(org_name)
        time.sleep(5)
        self.wait_elevisble(AdminPUUserOrgLocator.operation_button, doc)
        self.move_mouse_to_click_element(AdminPUUserOrgLocator.operation_button, doc)
        time.sleep(2)
        self.get_operation_name_click('Edit Role')
        time.sleep(5)
        self.click_element(AdminPUUserOrgLocator.edit_org_role_select, doc)
        time.sleep(4)
        # eles = self.get_elements(AdminPUUserOrgLocator.select_choices, doc)
        # eles[-1].click()
        self.scroll_bar_operation(AdminPUUserOrgLocator().get_edit_org_user_role(role_name))
        time.sleep(2)
        self.click_element(AdminPUUserOrgLocator().get_edit_org_user_role(role_name), doc)
        time.sleep(2)
        self.click_element(AdminPUUserOrgLocator.edit_org_confirm, doc)
        self.wait_elevisble(AdminPUUserOrgLocator.add_success_prompt, doc)

    def list_columns(self):
        doc = "验证admin端Organization列表字段"

        self.enter_the_org_page()
        self.wait_elevisble(AdminPUUserOrgLocator.list_columns, doc)
        list_columns = self.get_elements(AdminPUUserOrgLocator.list_columns, doc)
        assert list_columns[0].text == "Account Email"
        assert self.get_element(AdminPUUserOrgLocator.list_column_status, doc).text == "Status"
        assert list_columns[2].text == "User Name"
        assert list_columns[3].text == "Phone Number"
        assert list_columns[4].text.startswith("Creation Time")
        assert "Actions" in list_columns[5].text

        self.wait_elevisble(AdminPUUserOrgLocator.status_checkbox, doc)
        self.click_by_js(AdminPUUserOrgLocator.status_checkbox, doc)
        self.wait_elevisble(AdminPUUserOrgLocator.status_checkbox_options, doc)
        status_options = self.get_elements(AdminPUUserOrgLocator.status_checkbox_options, doc)
        assert status_options[0].text == "Normal"
        assert status_options[1].text == "Locked"
        assert status_options[2].text == "Inactive"

        print(self.get_element(AdminPUUserOrgLocator.reset_pwd_button, doc).text)
        assert self.get_element(AdminPUUserOrgLocator.reset_pwd_button, doc).text == "Reset Password"

        self.move_mouse_to_element(AdminPUUserOrgLocator.user_options_button)

        assert self.get_element(AdminPUUserOrgLocator.edit_organization_button, doc).text == "Edit Organization"
        assert self.get_element(AdminPUUserOrgLocator.edit_user_group_button, doc).text == "Edit User Group"
        assert self.get_element(AdminPUUserOrgLocator.edit_role_button, doc).text == "Edit Role"
        assert self.get_element(AdminPUUserOrgLocator.lock_button, doc).text == "Lock"
        assert self.get_element(AdminPUUserOrgLocator.unlock_button, doc).text == "Unlock"
        assert self.get_element(AdminPUUserOrgLocator.delete_button, doc).text == "Delete"
        assert self.get_element(AdminPUUserOrgLocator.resend_button, doc).text == "Resend"

    # 点击email跳转至用户具体信息界面
    def click_email(self):
        time.sleep(3)
        self.refresh_page()
        doc = "点击email进入用户具体信息界面"
        email_text = self.get_element(AdminPUUserOrgLocator.immediate_email, doc).text
        self.click_by_js(AdminPUUserOrgLocator.immediate_email, doc)

        time.sleep(3)
        self.wait_elevisble(AdminPUUserGroupLocator.user_info_page_title, doc)
        assert self.get_element(AdminPUUserGroupLocator.user_info_page_title, doc).text == email_text

    # 通过status字段的选项查询列表
    def query_by_status(self, num, is_reset=False):

        doc = "query by status value"
        self.enter_the_org_page()

        self.wait_elevisble(AdminPUUserOrgLocator.status_checkbox, doc)
        self.click_by_js(AdminPUUserOrgLocator.status_checkbox, doc)
        self.wait_elevisble(AdminPUUserOrgLocator.status_checkbox_options, doc)

        status_choices_elements = self.get_elements(AdminPUUserOrgLocator.status_choices, doc)

        choices = random.sample(status_choices_elements, num)

        print(choices in status_choices_elements)
        print(status_choices_elements)
        print(choices)
        status_chosen = []
        if num != 0 and is_reset is False:
            for ele in choices:
                time.sleep(1)
                if status_choices_elements.index(ele) == 0:
                    status_chosen.append('Normal')
                elif status_choices_elements.index(ele) == 1:
                    status_chosen.append('Locked')
                elif status_choices_elements.index(ele) == 2:
                    status_chosen.append('Inactive')
                ele.click()
        elif num == 0 or is_reset is True:
            status_chosen.extend(['Normal', 'Locked', 'Inactive'])

        if is_reset:
            time.sleep(2)
            self.click_element(AdminPUUserOrgLocator.status_reset, doc)

        self.click_element(AdminPUUserOrgLocator.status_confirm, doc)

        self.wait_elevisble(AdminPUUserOrgLocator.loading_wait, doc)
        self.wait_eleinvisble(AdminPUUserOrgLocator.loading_wait, doc)
        list_status = self.get_elements(AdminPUUserOrgLocator.filter_status, doc)
        if len(list_status) != 0:
            for ele in list_status:
                assert ele.text in status_chosen

        self.refresh_page()

    def get_by_status(self, status, doc):
        self.click_by_js(AdminPUUserOrgLocator.status_checkbox, doc)
        self.wait_elevisble(AdminPUUserOrgLocator.status_checkbox_options, doc)

        if status == "Normal":
            ele = self.get_elements(AdminPUUserOrgLocator.status_choices, doc)[0]
        elif status == "Locked":
            ele = self.get_elements(AdminPUUserOrgLocator.status_choices, doc)[1]
        elif status == "Inactive":
            ele = self.get_elements(AdminPUUserOrgLocator.status_choices, doc)[2]
        ele.click()

        self.click_element(AdminPUUserOrgLocator.status_confirm, doc)

        self.wait_elevisble(AdminPUUserOrgLocator.loading_wait, doc)
        self.wait_eleinvisble(AdminPUUserOrgLocator.loading_wait, doc)

    def lock_or_unlock_account(self):

        doc = "lock or unlock account"
        self.enter_the_org_page()

        self.get_by_status("Normal", doc)

        self.move_mouse_to_element(AdminPUUserOrgLocator.user_options_button)
        self.click_element(AdminPUUserOrgLocator.lock_area, doc)

        self.wait_elevisble(AdminPUUserOrgLocator.delete_org_ack, doc)
        self.click_by_js(AdminPUUserOrgLocator.delete_org_ack, doc)
        time.sleep(2)
        self.click_element(AdminPUUserOrgLocator.edit_org_confirm, doc)

        self.wait_elevisble(AdminPUUserOrgLocator.success_pup, doc)

        assert self.get_element(AdminPUUserOrgLocator.success_pup, doc).text == "Locked successfully."

        self.refresh_page()
        time.sleep(5)

        self.get_by_status("Locked", doc)

        self.move_mouse_to_element(AdminPUUserOrgLocator.user_options_button)
        self.click_element(AdminPUUserOrgLocator.unlock_area, doc)

        self.wait_elevisble(AdminPUUserOrgLocator.delete_org_ack, doc)
        self.click_by_js(AdminPUUserOrgLocator.delete_org_ack, doc)
        time.sleep(2)
        self.click_element(AdminPUUserOrgLocator.edit_org_confirm, doc)

        self.wait_elevisble(AdminPUUserOrgLocator.success_pup, doc)

        assert self.get_element(AdminPUUserOrgLocator.success_pup, doc).text == "Unlocked successfully."
